Learning JavaScript by Unknown

Learning JavaScript by Unknown

Author:Unknown
Language: eng
Format: mobi
Publisher: O'Reilly Media, Inc.
Published: 2016-02-29T16:00:00+00:00


The Fibonacci sequence goes on forever. And our application doesn’t know how many elements will be needed, which makes this an ideal application for iterators. The only difference between this and previous examples is that this iterator will never return true for done:

class FibonacciSequence { [Symbol.iterator]() { let a = 0, b = 1; return { next() { let rval = { value: b, done: false }; b += a; a = rval.value; return rval; } }; } }

If we used an instance of FibonacciSequence with a for...of loop, we’ll end up with an infinite loop…we’ll never run out of Fibonacci numbers! To prevent this, we’ll add a break statement after 10 elements:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.